package com.tzxs.pay.main.onlineaut;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.CalendarView;
import android.widget.TextView;
import android.widget.Toast;

import com.tzxs.pay.R;
import com.tzxs.pay.databinding.ActivityOnlineAusBinding;
import com.tzxs.pay.main.base.BaseActivity;
import com.tzxs.pay.util.StatusBarColorSetter;

//在线预授权订单查询
public class OnlineAutSActivity extends BaseActivity<ActivityOnlineAusBinding> {
    private String code;//凭证号
    private String orderId;//订单号
    private String name;//入住人
    private String phone;//入住人手机号
    private String startTime;//开始时间
    private String endTime;//结束时间
    private String orderPhone;//预定号码
    private static final int SEARCH_CODE = 100;

    @Override
    protected Activity getChildActivity() {
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_online_aus;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarColorSetter.setStatusBarColor(this, Color.parseColor("#FAFAFA"));
        dataBinding.startTime.setOnClickListener(v -> {
            chooseTime(dataBinding.startTime);
        });
        dataBinding.endTime.setOnClickListener(v -> {
            chooseTime(dataBinding.endTime);
        });
        dataBinding.onlineAusToolbar.setNavigationOnClickListener(v -> onBackPressed());
        dataBinding.searchBtn.setOnClickListener(v -> {
            //凭证号
            code = dataBinding.certificateCode.getText().toString();
            //订单号
            orderId = dataBinding.orderId.getText().toString();
            //入住人名
            name = dataBinding.inPeopleName.getText().toString();
            //入住人手机号
            phone = dataBinding.inPeoplePhone.getText().toString();
            //预定号码
            orderPhone = dataBinding.orderPhone.getText().toString();
            //时间
            startTime = dataBinding.startTime.getText().toString();
            endTime = dataBinding.endTime.getText().toString();
            if (TextUtils.isEmpty(code) && TextUtils.isEmpty(orderId) &&
                    TextUtils.isEmpty(name) && TextUtils.isEmpty(phone) &&
                    TextUtils.isEmpty(startTime) && TextUtils.isEmpty(endTime)) {
                Toast.makeText(this, "请输入至少一个搜索条件", Toast.LENGTH_SHORT).show();
                return;
            }
            Intent intent = new Intent(this, OnlineAutSListActivity.class);
            intent.putExtra("code", code);
            intent.putExtra("orderId", orderId);
            intent.putExtra("name", name);
            intent.putExtra("phone", phone);
            intent.putExtra("startTime", startTime);
            intent.putExtra("endTime", endTime);
            intent.putExtra("orderPhone", orderPhone);
            startActivityForResult(intent, SEARCH_CODE);
        });
    }

    /**
     * 选择时间
     */
    private void chooseTime(TextView textView) {
        final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
        View content = getLayoutInflater().inflate(R.layout.time_choose, null);
        CalendarView calendarView = content.findViewById(R.id.order_time_calendarView);
        bottomSheetDialog.setContentView(content);
        bottomSheetDialog.show();
        calendarView.setOnDateChangeListener((view, year, month, dayOfMonth) -> {
            bottomSheetDialog.dismiss();
            String mM;
            String day;
            if (dayOfMonth < 10) {//日
                day = "0" + String.valueOf(dayOfMonth);
            } else {
                day = String.valueOf(dayOfMonth);
            }
            if (month < 10) {
                mM = "0" + String.valueOf(month + 1);
            } else {
                mM = String.valueOf(month + 1);
            }
            String time = year + "-" + mM + "-" + day;
            textView.setText(time);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SEARCH_CODE) {
            dataBinding.certificateCode.setText("");
            dataBinding.orderId.setText("");
            dataBinding.inPeopleName.setText("");
            dataBinding.inPeoplePhone.setText("");
            dataBinding.startTime.setText("");
            dataBinding.endTime.setText("");
            dataBinding.orderPhone.setText("");
        }
    }
}